package com.supermap.search.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.supermap.search.R;
import com.supermap.search.adapter.MyAdapter;
import com.supermap.search.javabean.XunJianOneTime;
import com.supermap.search.utils.TimeUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class YanZhengTaskActivity extends AppCompatActivity {


    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;


    private List<XunJianOneTime> list;
    private MyAdapter<XunJianOneTime> adapter;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            swipeLayout.setRefreshing(false);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xun_jian_task);
        ButterKnife.bind(this);
        list = new ArrayList<>();
        init();
    }


    public void init() {
        initToolbar();
        initData();
        initSwipe();

    }

    private void initToolbar() {
        toolbarTitle.setText("验证任务");
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayShowTitleEnabled(false);
        toolbar.setNavigationIcon(R.drawable.back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initData() {
        XunJianOneTime bean1 = new XunJianOneTime(001L, "001", "严重", "灭火去不能使用", "", "电梯设备", "", "", "1", "113.301030", "23.400735", TimeUtils.date2String(new Date()), TimeUtils.date2String(new Date()), TimeUtils.date2String(new Date()));
        XunJianOneTime bean2 = new XunJianOneTime(001L, "001", "无影响", "正常", "", "电梯设备", "", "", "0", "113.300003", "23.399927", TimeUtils.date2String(new Date()), TimeUtils.date2String(new Date()), TimeUtils.date2String(new Date()));
        XunJianOneTime bean3 = new XunJianOneTime(001L, "002", "无影响", "正常","", "电梯设备", "", "", "0", "113.300476", "23.399936", TimeUtils.date2String(new Date()), TimeUtils.date2String(new Date()), TimeUtils.date2String(new Date()));
        XunJianOneTime bean4 = new XunJianOneTime(001L, "002","严重", "光缆内线暴露","", "电梯设备", "已修复,请领导检查", "", "2", "113.300003", "23.400975", TimeUtils.date2String(new Date()), TimeUtils.date2String(new Date()), TimeUtils.date2String(new Date()));
        list.add(bean1);
        list.add(bean2);
        list.add(bean3);
        list.add(bean4);
        if(adapter==null){
            adapter = new MyAdapter<XunJianOneTime>(list, R.layout.item_lv_noshangchuanyanzheng){

                @Override
                public void bindView(ViewHolder holder, XunJianOneTime obj) {
                    holder.setImageUrl(R.id.iv_noupxunjian, R.mipmap.default_img);
                    holder.setText(R.id.tv_yanzhongchengdu,"严重程度:" + obj.getYanzhongchengdu());
                    holder.setText(R.id.tv_wentiqingkuang,"问题情况:" + obj.getWentiqingkuang());
                    holder.setText(R.id.tv_reportTime,"上报时间:"+obj.getReportTime());
                    holder.setText(R.id.tv_repairTime,"维修时间:"+obj.getRepairTime());
                    holder.setText(R.id.tv_verifyTime,"验证时间:"+obj.getVerifyTime());
                    holder.setText(R.id.tv_yanzhengmiaoshu,"验证描述:"+obj.getVerifyDesc());
                    holder.setText(R.id.tv_weixiumiaoshu,"维修描述:"+obj.getRepairDesc());
                }
            };
            listview.setAdapter(adapter);
        }else{
            adapter.notifyDataSetChanged();
        }

    }

    private void initSwipe() {
        swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                initData();
                handler.sendEmptyMessageDelayed(0,2000);

            }
        });


        swipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);
//        SwipeLy.setDistanceToTriggerSync(400);// 设置手指在屏幕下拉多少距离会触发下拉刷新
        swipeLayout.setSize(SwipeRefreshLayout.DEFAULT); // 设置圆圈的大小
    }
}
